# -*- encoding: utf-8 -*-
import json
from Common.mysql import app
from flask import jsonify, request
from src.User.token import token_verify, token_user_id, before_request
from Common.yaml_method import YamlMethod
from src.ApiTest.Operating.Function.add_operating import AddOperatingConfig
from src.ApiTest.Operating.Function.operating_list import OperatingList
from src.ApiTest.Operating.Function.update_operating import UpdateOperatingConfig
from src.ApiTest.Operating.Function.delete_operating import DeleteOperatingConfig

code = YamlMethod().read_data('code.yaml')['code']


@app.route('/operating/add', methods=['POST'])
@token_verify
def add_operating(create_user):
    """
    添加运行配置接口
    :param create_user:
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    config_name = data['configName']
    project_data = data['projectName']
    if '/' in project_data:
        project_data = data['projectName'].split('/')
        project_name = project_data[0]
        project_id = project_data[1]
    else:
        project_name = project_data
        project_id = data['projectId']
    environment = data['environment']

    # 校验参数
    if not all([config_name, project_id, project_name, environment]):
        return jsonify(code=code[7], msg="信息不完整")

    # 提交信息
    res = AddOperatingConfig().add_operating_config(config_name, project_id, project_name, environment, create_user)

    return jsonify(res)


@app.route('/operating/list', methods=['POST'])
@token_user_id
def operating_list(user_id):
    """
    获取运行配置列表接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    page = data['page']
    limit = data['limit']
    project_name = data['projectName']

    res = OperatingList().operating_list(page, limit, user_id, project_name)

    return res


@app.route('/operating/update', methods=['POST'])
@token_verify
def operating_update(update_user):
    """
    更新运行配置信息接口
    :param update_user: 操作用户名
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    config_id = data['id']
    config_name = data['configName']
    project_data = data['projectName']
    if '/' in project_data:
        project_data = data['projectName'].split('/')
        project_name = project_data[0]
        project_id = project_data[1]
    else:
        project_name = project_data
        project_id = data['projectId']
    environment = data['environment']

    # 校验参数
    if not all([config_name, project_id, project_name, environment]):
        return jsonify(code=code[7], msg="信息不完整")

    res = UpdateOperatingConfig().update_operating_config(config_id, config_name, project_id, project_name, environment,
                                                          update_user)
    return res


@app.route('/operating/delete', methods=['POST'])
@before_request
def operating_delete():
    """
    删除运行配置信息接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    config_id = data['id']

    res = DeleteOperatingConfig().delete_operating_config(config_id)
    return res
